package com.maven.front.controller.manageLogin;

import com.maven.front.bean.Result;
import com.maven.front.bean.SysFunction;
import com.maven.front.bean.SysRole;
import com.maven.front.bean.SysRoleFunction;
import com.maven.front.service.manageLogin.PermissionService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("admin/permission")
public class PermissionController {


    @Resource
    private PermissionService permissionService;

    @GetMapping("permission")
    public Result permission(@RequestHeader("token2") String token2) {
        return this.permissionService.selectById(token2);
    }

    @GetMapping("selectId")
    public Result selectId() {
        List<SysFunction> list = this.permissionService.selectId();
        if (CollectionUtils.isEmpty(list)) {
            return new Result(201, "无数据");
        } else {
            return new Result(200, "success", list);
        }
    }

    @GetMapping("selectRole")
    public Result selectRole(SysRole sysRole) {
        return this.permissionService.selectRole(sysRole);
    }


    @PostMapping("insertRole")
    public Result insertRole(SysRole sysRole) {

        return this.permissionService.insertRole(sysRole);
    }

      //查询所有权限
     @GetMapping("selectAll")
    public Result selectAll(){
       return this.permissionService.selectAll();
    }

    //      @GetMapping("selRoleId")
//     public Result selRoleId(SysRole sysRole){
//                return this.permissionService.selRoleId(sysRole);
//     }
    //增加权限
    @PostMapping("insertRoleFunction")
    public Result insertRoleFunction(String roleId, String functionId) {
        //先删除已拥有的权限
        this.permissionService.deleteRole(Integer.valueOf(roleId));
        List<SysRoleFunction> list = new ArrayList<SysRoleFunction>();
        String funid[] = functionId.split(",");
        for (String i : funid) {
            if (!StringUtils.isEmpty(i)) {
                SysRoleFunction sysRoleFunction = new SysRoleFunction();
                sysRoleFunction.setRoleId(Integer.valueOf(roleId));
                sysRoleFunction.setFunctionId(Integer.valueOf(i));
                list.add(sysRoleFunction);
            }
        }
        return this.permissionService.insertRoleFunction(list);
    }

    //查询单个id的所有权限
    @GetMapping("selectgetById")
    public Result selectgetById(Integer roleId) {
        return this.permissionService.selectgetById(roleId);
    }

    //删除已拥有的权限
    @PostMapping("deleteRole")
    public Result deleteRole(Integer roleId) {
        return this.permissionService.deleteRole(roleId);
    }

     //添加父权限
      @PostMapping("insertFunction")
     public Result insertFunction(SysFunction sysFunction){
          sysFunction.setParentId(0);
          sysFunction.setCreateTime(new Date());
        int res=this.permissionService.insertFunction(sysFunction);
        if(res>0){
            return new Result(200,"success",sysFunction.getFunctionId());
        }else{
            return new Result(201,"失败");
        }
     }

      //添加子权限
    @PostMapping("insertFunctionz")
      public Result insertFunctionz(SysFunction sysFunction){
        sysFunction.setCreateTime(new Date());
        int res=this.permissionService.insertFunctionz(sysFunction);
        if(res>0){
            return new Result(200,"success",true);
        }else{
            return new Result(201,"添加失败");
        }

      }
}
